﻿/// <reference path="knockout-2.2.1.js" />

function MenuViewModel(menuData, ordersHistory) {
    // simple knockout.js functionality
    // http://learn.knockoutjs.com/#/?tutorial=collections - I've read few of these

    // class definitions
    var Category = Class.create({
        init: function (categoryName) {
            this.categoryName = categoryName;
            this.data = [];
        }
    });
    var MenuItem = Class.create({
        init: function (menuItemData) {
            for (var key in menuItemData) {
                this[key] = menuItemData[key];
            }
        }
    });
    var OrderItem = Class.create({
        init: function (id, quantity) {
            this.id = id;
            this.quantity = ko.observable(quantity);
            this.totalPrice = ko.observable(self.itemsMap[this.id] * this.quantity);
        }
    });

    // Data
    var self = this;
    self.menuData = [];
    self.itemsMap = {};
    self.promotionItems = new Array();
    for (var i = 0; i < menuData.length; i++) {
        var currentCategory = new Category(menuData[i].categoryName);
        for (var j = 0; j < menuData[i].data.length; j++) {
            var currentMenuItem = new MenuItem(menuData[i].data[j]);
            currentCategory.data.push(currentMenuItem);
            self.itemsMap[currentMenuItem.id] = currentMenuItem;
			if(currentMenuItem.isPromotion === true){
			    self.promotionItems.push({ id: menuData[i].data[j].id });
			}
        }
        self.menuData.push(currentCategory);
    }
    self.info = {
        src: ko.observable("img/salad.png"),
        name: ko.observable("Salad 1"),
        price: ko.observable(4.59),
        time: ko.observable(4),
        description: ko.observable("some text goes here"),
        id: ko.observable(100)
    };

    self.ordersHistory = ko.observableArray(ordersHistory);

    self.order = ko.observableArray([]);
    self.totalPrice = ko.computed(function () {
        var total = 0;
        var orderItemTotalPrice = 0;
        for (var i = self.order().length - 1; i >= 0; i--) {
            var currentOrderItem = self.order()[i];
            var currentMenuItem = self.itemsMap[currentOrderItem.id];
            if (currentOrderItem.quantity() < 1) {
                self.order.remove(currentOrderItem);
            }
            else {
                currentOrderItem.totalPrice(currentMenuItem.price * currentOrderItem.quantity());
                total += currentOrderItem.totalPrice();
            }
        }
        return total;
    });

    // Behaviour
    self.changeInfo = function(menuItemId) {
        self.info.src(self.itemsMap[menuItemId].imgSrc);
        self.info.name(self.itemsMap[menuItemId].name);
        self.info.price(self.itemsMap[menuItemId].price);
        self.info.time(self.itemsMap[menuItemId].time);
        self.info.description(self.itemsMap[menuItemId].description);
        self.info.id(menuItemId);
    };

    self.addOrderItem = function (menuItemId, quantity) {
        var menuItem = self.itemsMap[menuItemId];
        // is already added
        for (var i = 0; i < self.order().length; i++) {
            if (self.order()[i].id === menuItemId) {
                self.order()[i].quantity(self.order()[i].quantity() + 1);
                return;
            }
        }

        quantity = quantity || 1;
        var orderItem = new OrderItem(menuItem.id, quantity);
        self.order.push(orderItem);
    }

    self.removeOrderItem = function (orderItem) {
        self.order.remove(orderItem);
    }

    self.clearOrder = function () {
        self.order([]);
    }

    self.getOrderData = function () {
        var orderData = []
        for (var i = 0; i < self.order().length; i++) {
            var currentOrderItem = self.order()[i];
            var dataItem = {
                id: currentOrderItem.id,
                quantity: currentOrderItem.quantity()
            }

            orderData.push(dataItem);
        }

        var orderDate = Date();

        var order = {
            orderDate: orderDate,
            data: orderData
        };

        return order;
    }

    self.updateOrdersHistory = function (ordersHistory) {
        self.ordersHistory(ordersHistory);
    }

    self.reloadOrder = function (historyOrderId) {
        var ordersHistory = self.ordersHistory();
        for (var i = 0; i < ordersHistory.length; i++) {
            if (ordersHistory[i].orderDate === historyOrderId) {
                var currentHistoryOrder = ordersHistory[i];
                break;
            }
        }

        self.clearOrder();
        for (var i = 0; i < currentHistoryOrder.data.length; i++) {
            var menuItem = currentHistoryOrder.data[i];
            self.addOrderItem(menuItem.id, menuItem.quantity);
        }
    }
}